using System;

namespace Framework
{
    public class UIWindow : UIWidget
    {
        public UGUIWindow Window { get; private set; }
        public string Path;
        public bool IsFocus { get; private set; }
        private object _sign;
        private Action _closeAction;
        public sealed override void Bind(UGUIWidget value)
        {
            if (Widget != null)
            {
                return;
            }
            base.Bind(value);
            if (this.Window != null) return;
            if (this.Widget is UGUIWindow window)
            {
                this.Window = window;
            }
        }
        public void Awake(object sign)
        {
            if (_sign != null && _sign != sign) return;
            OnAwake();
            Enabled = true;
        }
        public void Update(object sign, float deltaTime)
        {
            if (_sign != null && _sign != sign) return;
            OnUpdate(deltaTime);
        }
        public void SetScreenInfo(object sign, UGUIScreenInfo value)
        {
            if (_sign != null && _sign != sign)
            {
                FrameworkLog.LogError("权限出错");
                return;
            }
            OnScreenInfoChanged(value);
        }
        public void SetStatus(object sign, UIStatus value, UIStatus previous)
        {
            if (_sign != null && _sign != sign)
            {
                FrameworkLog.LogError("权限出错");
                return;
            }
            if (UIStatus.CloseAfter == value)
            {
                if (_closeAction != null)
                {
                    _closeAction.Invoke();
                    _closeAction = null;
                }
            }
            OnStatusChanged(value, previous);
        }
        public void Blur(object sign)
        {
            if (_sign != null && _sign != sign)
            {
                //FrameworkLog.LogError("权限出错");
                return;
            }
            IsFocus = false;
            //FrameworkLog.LogFormat("Blur {0}", this.Path);
            OnBlur();
        }
        public void Focus(object sign)
        {
            if (_sign != null && _sign != sign)
            {
                //FrameworkLog.LogError("权限出错");
                return;
            }
            IsFocus = true;
            //FrameworkLog.LogFormat("Focus {0}", this.Path);
            OnFocus();
        }
        public virtual void OnSortingOrderChanged(int value, int oldValue)
        {

        }
        protected virtual void OnBlur()
        {

        }
        protected virtual void OnFocus()
        {

        }
        protected virtual void OnStatusChanged(UIStatus value, UIStatus previous)
        {

        }
        protected virtual void OnScreenInfoChanged(UGUIScreenInfo value)
        {

        }
        protected virtual void OnUpdate(float deltaTime)
        {

        }
        protected void Close(Action closeAction = null)
        {
            _closeAction = closeAction;
            if (this.Window != null)
            {
                this.Window.Close();
            }
        }
    }
}